//
//  YWWorkReminderManager.m
//  maintain
//
//  Created by Kyo on 16/6/27.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWWorkReminderManager.h"
#import "YWWorkReminderDBManager.h"
#import "YWCommonTool.h"
#import "MJExtension.h"
#import "YWMacro.h"
#import "YWConst.h"

#import "YWLoginUser.h"
#import "YWLoginTool.h"
#import "YWNotifyManager.h"
#import "YWPush.h"
#import "YWMissionManager.h"
#import "YWKeepWatchMissionManager.h"
#import "YWInspectionMissionManager.h"
#import "YWCirculationInfo.h"
#import "YWCirculationManager.h"
#import "YWMaterialApply.h"
#import "YWMaterialManager.h"
#import "YWEwinManager.h"
#import "WMHttpTool.h"
#import "YWReminder.h"
#import "YWReminderList.h"
#import "YWDetectAndUpkeepManager.h"
#import "YWTroubleMissionModel.h"
#import "YWTroubleMissionManager.h"
#import "YWReportTroubleModel.h"
#import "YWReportTroubleManager.h"
#import "YWDailyReport.h"

@implementation YWWorkReminderManager
YWSingletonM(Manager)

//- (BOOL)updateWorkReminder:(YWWorkReminder*)workReminder{
//    return [[YWWorkReminderDBManager shareManager] updateWorkReminder:workReminder];
//}
//
//- (BOOL)updateWorkReminderWithArray:(NSArray*)workReminderArray{
//    return [[YWWorkReminderDBManager shareManager] updateWorkReminderWithArray:workReminderArray];
//}
//
//- (YWWorkReminder*)theLatestWorkReminder{
//    return [[YWWorkReminderDBManager shareManager] theLatestWorkReminder];
//}
//
//- (NSArray *)workReminderWithPage:(NSInteger)page{
//    return [[YWWorkReminderDBManager shareManager] workReminderWithPage:page];
//}
//
//- (BOOL)updateReadStatusWithWorkReminder:(YWWorkReminder*)workReminder{
//    return [[YWWorkReminderDBManager shareManager] updateReadStatusWithWorkReminder:workReminder];
//}

- (void)saveReminderWithPushMessage:(id)pushMessage{
    //任务进度(工作提醒只显示完成的)
    if ([pushMessage isKindOfClass:[YWPushMission class]]) {
        YWPushMission *pushMission = (YWPushMission *)pushMessage;
        if (pushMission.completedQuantity >= pushMission.equipmentQuantity) {
            dispatch_async(dispatch_get_main_queue(), ^{
                //[self saveAndPostNotificationWithReminder:reminder];
                if (pushMission.executor.uniqueId!=[YWLoginTool loginUser].uniqueId) {
                    if (pushMission.maintenanceTypeId==YWMaintenanceTypeDetection) {
                        [[YWEwinManager shareManager] subtractTotalCountWithType:YWUnReadCountTypeDetection];
                    }else{
                        [[YWEwinManager shareManager] subtractTotalCountWithType:YWUnReadCountTypeUpkeep];
                    }
                }
            });
        }
        if (pushMission.missionId) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [YWDetectAndUpkeepManager requestMissionWithMissionId:pushMission.missionId completeBlock:nil];
                YWMaintenanceMission *mission = [[YWMaintenanceMission alloc] init];
                mission.missionId = pushMission.missionId;
                mission.maintenanceTypeId = pushMission.maintenanceTypeId;
                [YWDetectAndUpkeepManager requestMissionRecordWithMission:mission completeBlock:nil];
            });
        }
        
    }else if ([pushMessage isKindOfClass:[YWPushTrouble class]]){
        //完成维修
        YWPushTrouble *pushTrouble = (YWPushTrouble *)pushMessage;
        [[YWCirculationManager shareManager] requestCirculationInfoWithTroubleId:pushTrouble.troubleId needTip:NO WithComplete:^(BOOL success) {
            YWTroubleMissionModel * troubleMissionModel = [[YWTroubleMissionManager shareManager] troubleMissionModelWithTroubleId:pushTrouble.troubleId];
            if (troubleMissionModel.executor.uniqueId != [YWLoginTool loginUser].uniqueId) {
                [[YWEwinManager shareManager] subtractTotalCountWithType:YWUnReadCountTypeFixTrouble];
            }
        }];
        
    }else if ([pushMessage isKindOfClass:[YWPushAssign class]]){
        YWPushAssign *pushAssign = (YWPushAssign *)pushMessage;
        [[YWCirculationManager shareManager] requestCirculationInfoWithTroubleId:pushAssign.troubleId needTip:NO WithComplete:^(BOOL success) {
            YWTroubleMissionModel * troubleMissionModel = [[YWTroubleMissionManager shareManager] troubleMissionModelWithTroubleId:pushAssign.troubleId];
            NSInteger currentUserId = [YWLoginTool loginUser].uniqueId;
            if (troubleMissionModel && troubleMissionModel.troubleStatus == YWTroubleMissionStatusUnAssign && troubleMissionModel.executor.uniqueId == currentUserId) {
                //维修任务未读数
                [YWLoginTool saveMissionNoRedCountWithMaintenanceType:YWMaintenanceTypeTrouble];
                [[YWNotifyManager shareManager] postRequestTroubleMissionNotify:pushAssign.troubleId];
            }
            if (troubleMissionModel && (troubleMissionModel.executor.uniqueId == currentUserId || troubleMissionModel.creator.uniqueId == currentUserId ||[troubleMissionModel.trouble.observerIds containsString:[@(currentUserId) stringValue]])) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[YWEwinManager shareManager] addTotalCountWithType:YWUnReadCountTypeFixTrouble];
                    [[NSNotificationCenter defaultCenter] postNotificationName:KPostAssignTroubleBillNotification object:nil];
                });
            }
        }];
    }else if ([pushMessage isKindOfClass:[YWPushReport class]]){
        YWPushReport *pushReport = (YWPushReport *)pushMessage;
        [[YWCirculationManager shareManager] requestCirculationInfoWithTroubleId:pushReport.troubleId needTip:NO WithComplete:^(BOOL success) {
            YWReportTroubleModel * reportTrouble = [[YWReportTroubleManager shareManager] getOneReportTroubleWithTroubleId:pushReport.troubleId];
            if (reportTrouble.reporter.uniqueId!=[YWLoginTool loginUser].uniqueId) {//报障人不是当前登录者，应该抢单+1
                [[YWEwinManager shareManager] addTotalCountWithType:YWUnReadCountTypeUnDistributionTroubleTotal];
                if ([YWLoginTool loginUser].isTroubleServer) {//如果是派单员，则报障单+1
                    [[YWEwinManager shareManager] addTotalCountWithType:YWUnReadCountTypeTrouble];
                }
            }
        }];
    }else if ([pushMessage isKindOfClass:[YWPushCommonMission class]]){
        YWPushCommonMission *commonMission = (YWPushCommonMission *)pushMessage;
        if (commonMission.maintenanceTypeId == YWMaintenanceTypeDetection) {
            [[YWEwinManager shareManager] addTotalCountWithType:YWUnReadCountTypeDetection];
        }else{
            [[YWEwinManager shareManager] addTotalCountWithType:YWUnReadCountTypeUpkeep];
        }
        [YWDetectAndUpkeepManager requestMissionWithMissionId:commonMission.missionId completeBlock:nil];
    }
    //临时任务-----通知-----评论
    else if ([pushMessage isKindOfClass:[YWPushCancelMission class]]){
        YWPushCancelMission *pushCancelMission = (YWPushCancelMission *)pushMessage;
        //修改任务为删除状态
        [[YWMissionManager shareManager] mofifyMissionStatusWithMissionId:pushCancelMission.missionId maintenanceType:pushCancelMission.maintenanceTypeId];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self saveAndPostNotificationWithReminder:nil];
        });
        
    }else if ([pushMessage isKindOfClass:[YWPushCancelPostTrouble class]]){
        //	取消报障
        YWPushCancelPostTrouble *cancelPostTrouble = (YWPushCancelPostTrouble *)pushMessage;
        if (cancelPostTrouble.troubleId>0) {
            [[YWCirculationManager shareManager] requestCirculationInfoWithTroubleId:cancelPostTrouble.troubleId needTip:NO WithComplete:^(BOOL success) {
                //修改任务为取消报障状态
                [[YWMissionManager shareManager] mofifyMissionStatusWithMissionId:cancelPostTrouble.troubleId maintenanceType:YWMaintenanceTypeTrouble];
            }];
        }
    }else if ([pushMessage isKindOfClass:[YWPushCancelTroubleMission class]]){
        //	撤回维修任务
        YWPushCancelTroubleMission *cancelTroubleMission = (YWPushCancelTroubleMission *)pushMessage;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self saveAndPostNotificationWithReminder:nil];
        });
        if (cancelTroubleMission.troubleId>0) {
            [[YWCirculationManager shareManager] requestCirculationInfoWithTroubleId:cancelTroubleMission.troubleId needTip:NO WithComplete:nil];
        }
    }else if ([pushMessage isKindOfClass:[YWPushRejectTroubleMission class]]){
        //	退回维修任务
        YWPushRejectTroubleMission *rejectTroubleMission = (YWPushRejectTroubleMission *)pushMessage;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self saveAndPostNotificationWithReminder:nil];
        });
        if (rejectTroubleMission.troubleId>0) {
            [[YWCirculationManager shareManager] requestCirculationInfoWithTroubleId:rejectTroubleMission.troubleId needTip:NO WithComplete:nil];
        }
    }
    else if ([pushMessage isKindOfClass:[YWPushMissionModify class]]){
        YWPushMissionModify *missionModify = (YWPushMissionModify *)pushMessage;
        if (missionModify.missionId) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self saveAndPostNotificationWithReminder:nil];
            });
            [YWDetectAndUpkeepManager requestMissionWithMissionId:missionModify.missionId completeBlock:nil];
        }
    }else if ([pushMessage isKindOfClass:[YWPushKeepWatchMission class]]){
        YWPushKeepWatchMission *pushKeepWatchMission = (YWPushKeepWatchMission*)pushMessage;
        [[YWEwinManager shareManager] addTotalCountWithType:YWUnReadCountTypeKeepWatch];
        [[YWKeepWatchMissionManager shareManager] requestKeepWatchMissionWithMissionId:pushKeepWatchMission.keepWatchMissionId Complete:nil];
        
    }else if ([pushMessage isKindOfClass:[YWPushKeepWatchMissionProgress class]]){
        //巡更进度推送
        YWPushKeepWatchMissionProgress *keepWatchMissionProgress = (YWPushKeepWatchMissionProgress*)pushMessage;
        [[YWKeepWatchMissionManager shareManager] requestPatrolPointRecord:keepWatchMissionProgress.uniqueTag WithComplete:nil];
    }
    else if ([pushMessage isKindOfClass:[YWPushFinishKeepWatchMission class]]){
        //完成一次巡更的推送-----(铃铛显示)
        YWPushFinishKeepWatchMission *finishKeepWatchMission = (YWPushFinishKeepWatchMission*)pushMessage;
        dispatch_async(dispatch_get_main_queue(), ^{
//            [self postBellBellYellowSpotNotification];
            [[YWKeepWatchMissionManager shareManager] requestKeepWatchMissionInfo:finishKeepWatchMission.keepWatchMissionId complete:nil];
        });
        
    }else if ([pushMessage isKindOfClass:[YWPushModifyKeepWatchMission class]]){
        YWPushModifyKeepWatchMission *modifyKeepWatchMission = (YWPushModifyKeepWatchMission *)pushMessage;
        if (modifyKeepWatchMission.keepWatchMissionId) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self saveAndPostNotificationWithReminder:nil];
            });
            [[YWKeepWatchMissionManager shareManager] requestKeepWatchMissionWithMissionId:modifyKeepWatchMission.keepWatchMissionId Complete:nil];
        }
        
    }else if ([pushMessage isKindOfClass:[YWPushCancelKeepWatchMission class]]){
        YWPushCancelKeepWatchMission *cancelKeepWatchMission = (YWPushCancelKeepWatchMission *)pushMessage;
        [[YWEwinManager shareManager] subtractTotalCountWithType:YWUnReadCountTypeKeepWatch];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self saveAndPostNotificationWithReminder:nil];
            //修改巡更任务为删除状态
            [[YWKeepWatchMissionManager shareManager] updateKeepWatchDeleteStatusWithMissionId:cancelKeepWatchMission.keepWatchMissionId];
        });
        
    }else if ([pushMessage isKindOfClass:[YWPushInspectionMission class]]){
        YWPushInspectionMission *pushInspectionMission = (YWPushInspectionMission*)pushMessage;
        [[YWEwinManager shareManager] addTotalCountWithType:YWUnReadCountTypeInspection];
        [[YWInspectionMissionManager shareManager] requestInspectionMissionWithMissionId:pushInspectionMission.inspectionMissionId Complete:nil];
        
    }else if ([pushMessage isKindOfClass:[YWPushCancelInspectionMission class]]){
        YWPushCancelInspectionMission *cancelInspectionMission = (YWPushCancelInspectionMission *)pushMessage;
        [[YWEwinManager shareManager] subtractTotalCountWithType:YWUnReadCountTypeInspection];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self saveAndPostNotificationWithReminder:nil];
            //修改巡查任务为删除状态
            [[YWInspectionMissionManager shareManager] updateInspectionMissionDeleteStatusWithMissionId:cancelInspectionMission.inspectionMissionId];
        });
        
    }else if ([pushMessage isKindOfClass:[YWPushModifyInspectionMission class]]){
        YWPushModifyInspectionMission *modifyInspection = (YWPushModifyInspectionMission*)pushMessage;
        if (modifyInspection.inspectionMissionId) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self saveAndPostNotificationWithReminder:nil];
            });
            [[YWInspectionMissionManager shareManager] requestInspectionMissionWithMissionId:modifyInspection.inspectionMissionId Complete:nil];
        }
        
    }else if ([pushMessage isKindOfClass:[YWPushInspectionMissionProgress class]]){
        //巡查任务有新的进度（不显示出来，只请求同步记录）
        YWPushInspectionMissionProgress *missionProgress = (YWPushInspectionMissionProgress*)pushMessage;
        [[YWInspectionMissionManager shareManager] requestInspectionMissionInfo:missionProgress.inspectionMissionId complete:nil];
        
    }else if ([pushMessage isKindOfClass:[YWPushFinishInspectionMission class]]){
        //完成巡查任务-----(铃铛显示)
        YWPushFinishInspectionMission *finishMission = (YWPushFinishInspectionMission*)pushMessage;
        dispatch_async(dispatch_get_main_queue(), ^{
            [[YWInspectionMissionManager shareManager] requestInspectionMissionInfo:finishMission.inspectionMissionId complete:nil];
        });
        
    }else if ([pushMessage isKindOfClass:[KPushSystemKeepWatchMission class]]){
        //系统生成巡更记录
        KPushSystemKeepWatchMission *systemKeepwatch = (KPushSystemKeepWatchMission*)pushMessage;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self saveAndPostNotificationWithReminder:nil];
            [[YWKeepWatchMissionManager shareManager] requestKeepWatchMissionInfo:systemKeepwatch.keepWatchMissionId complete:nil];
        });
        
    }else if ([pushMessage isKindOfClass:[KPushSystemInspectionMission class]]){
        //系统生成巡查记录
        KPushSystemInspectionMission *systemInspection = (KPushSystemInspectionMission*)pushMessage;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self saveAndPostNotificationWithReminder:nil];
            [[YWInspectionMissionManager shareManager] requestInspectionMissionInfo:systemInspection.inspectionMissionId complete:nil];
        });
        
    }else if ([pushMessage isKindOfClass:[KPushMaterialApply class]]){
        //物料申请推送
        KPushMaterialApply *pushMaterialApply = (KPushMaterialApply*)pushMessage;
        [[YWMaterialManager shareManager] requestMaterialApplyWithApplyId:pushMaterialApply.applyId Complete:nil];
        
    }else if ([pushMessage isKindOfClass:[KPushMaterialApplyUpdate class]]){
        //物料申请抄送人
        KPushMaterialApplyUpdate *materialUpdate = (KPushMaterialApplyUpdate*)pushMessage;
        [[YWMaterialManager shareManager] requestMaterialApplyWithApplyId:materialUpdate.applyId Complete:nil];
        
    }else if ([pushMessage isKindOfClass:[KPushMaterialCancel class]]){
        //物料申请撤回
        KPushMaterialCancel *materialCancel = (KPushMaterialCancel*)pushMessage;
        YWMaterialApplyViewModel *materialApplyVieModel = [[YWMaterialManager shareManager] materialApplyWithMaterialApplyId:materialCancel.applyId];
        if (materialApplyVieModel) {
            [[YWMaterialManager shareManager] updateMaterialStatus:YWMaterialDataStatusCancel withApplyId:materialCancel.applyId];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (materialApplyVieModel.creator.uniqueId != [YWLoginTool loginUser].uniqueId) {
                    [YWLoginTool saveMaterialNoReadCount];
                    [[NSNotificationCenter defaultCenter] postNotificationName:KPostMaterialNoReadCountNotification object:nil];
                }
            });
        }
        
    }else if ([pushMessage isKindOfClass:[KPushMaterialReject class]]){
        //物料申请驳回
        KPushMaterialReject *materialReject = (KPushMaterialReject*)pushMessage;
        YWMaterialApplyViewModel *materialApplyVieModel = [[YWMaterialManager shareManager] materialApplyWithMaterialApplyId:materialReject.applyId];
        if (materialApplyVieModel) {
            [[YWMaterialManager shareManager] updateMaterialApplyStatus:YWMaterialStatusReject withApplyId:materialReject.applyId];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [YWLoginTool saveMaterialNoReadCount];
                [[NSNotificationCenter defaultCenter] postNotificationName:KPostMaterialNoReadCountNotification object:nil];
            });
        }
    }else if ([pushMessage isKindOfClass:[KPushMaterialStockOut class]]){
        //物料领用
        KPushMaterialStockOut *materialStockOut = (KPushMaterialStockOut*)pushMessage;
        [[YWMaterialManager shareManager] scanMaterialReceipientQrcodeWithStockOutId:materialStockOut.stockOutId creatorId:materialStockOut.creator.uniqueId isScan:NO WithComplete:^(BOOL success) {
            if (success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [YWLoginTool saveMaterialNoReadCount];
                    [[NSNotificationCenter defaultCenter] postNotificationName:KPostMaterialNoReadCountNotification object:nil];
                });
            }
        }];
    }else if ([pushMessage isKindOfClass:[KPushMaterialReturn class]]){
        //物料归还
        KPushMaterialReturn *materialReturn = (KPushMaterialReturn*)pushMessage;
        [[YWMaterialManager shareManager] scanMaterialReturnQrcodeWithReturnId:materialReturn.returnId creatorId:materialReturn.creator.uniqueId isScan:NO WithComplete:^(BOOL success) {
            if (success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [YWLoginTool saveMaterialNoReadCount];
                    [[NSNotificationCenter defaultCenter] postNotificationName:KPostMaterialNoReadCountNotification object:nil];
                });
            }
        }];
    }
}

- (void)saveAndPostNotificationWithReminder:(YWReminder*)reminder{
//    //保存数据库
//    BOOL save = [self updateWorkReminder:reminder];
//    //发通知更新最新消息
//    if (save) {
//        [[NSNotificationCenter defaultCenter] postNotificationName:KPostRefreshTheLatestWorkReminderNotification object:reminder];
//    }
    
    //提醒未读数字
    [YWLoginTool saveWorkReminderNoRedCount];
    [[NSNotificationCenter defaultCenter] postNotificationName:KPostWorkRmindUnReadCountNotification object:nil];
}

//- (void)postBellBellYellowSpotNotification{
//    [[NSNotificationCenter defaultCenter] postNotificationName:KPostShowBellYellowSpotNotification object:nil];
//}

//- (YWWorkReminder *)createMessageWithTitle:(NSString*)title content:(NSString *)content finishStatus:(NSInteger)finishStatus fromUserId:(NSInteger)fromUserId relationId:(NSString *)relationId relationType:(YWReminderRelationType)relationType{
//    YWWorkReminder *reminder = [[YWWorkReminder alloc] init];
//    
//    reminder.reminderId = [YWCommonTool randStringId];
//    reminder.title = title;
//    reminder.content = content;
//    reminder.createTime = [[NSDate date] timeIntervalSince1970]*1000;
//    
//    reminder.fromUserId = fromUserId;
//    YWLoginUser *currentUser = [YWLoginTool loginUser];
//    reminder.toUserId = currentUser.uniqueId;
//    reminder.finishStatus = finishStatus;
//    reminder.isRead = NO;
//    reminder.relationId = relationId;
//    reminder.relationType = relationType;
//    return reminder;
//}

//- (NSString *)relationTypeNameWithType:(NSInteger)relationType{
//    NSString* type = @"工作提醒";
//    switch (relationType) {
//        case Trouble_Report:
//            type = @"报障";
//            break;
//        case Trouble_Cancel_Report:
//            type = @"撤回报障";
//            break;
//        case Trouble_Assign:
//            type = @"报障分配";
//            break;
//        case Trouble_Cancel_Mission:
//            type = @"维修任务撤回";
//            break;
//        case Trouble_Reject_Mission:
//            type = @"维修任务退回";
//            break;
//        case Trouble_Mission_Done:
//            type = @"维修完成";
//            break;
//        case Trouble_Mission:
//            type = @"维修任务";
//            break;
//        case Detection_Mission_Assign:
//            type = @"检测任务";
//            break;
//        case Detection_Mission_Cancel:
//            type = @"检测任务撤回";
//            break;
//        case Detection_Mission_Modify:
//            type = @"检测任务变更";
//            break;
//        case Detection_Mission_Done:
//            type = @"检测任务完成";
//            break;
//        case Upkeep_Mission_Assign:
//            type = @"保养任务";
//            break;
//        case Upkeep_Mission_Cancel:
//            type = @"保养任务撤回";
//            break;
//        case Upkeep_Mission_Modify:
//            type = @"保养任务变更";
//            break;
//        case Upkeep_Mission_Done:
//            type = @"保养任务完成";
//            break;
//        case Inspection_Mission_Assign:
//            type = @"巡查任务";
//            break;
//        case Inspection_Mission_Cancel:
//            type = @"巡查任务撤回";
//            break;
//        case Inspection_Mission_Modify:
//            type = @"巡查任务变更";
//            break;
//        case KeepWatch_Mission_Assign:
//            type = @"巡更任务";
//            break;
//        case KeepWatch_Mission_Cancel:
//            type = @"巡更任务撤回";
//            break;
//        case KeepWatch_Mission_Modify:
//            type = @"巡更任务变更";
//            break;
//        case Material_Apply:
//            type = @"物料申请";
//            break;
//        case Material_Apply_Update:
//            type = @"物料申请";
//            break;
//        case Material_Apply_Cancel:
//            type = @"物料申请撤回";
//            break;
//        case Material_Apply_Reject:
//            type = @"物料申请驳回";
//            break;
//        case Material_Apply_StockOut:
//            type = @"物料领用";
//            break;
//        case Material_Apply_Return:
//            type = @"物料归还";
//            break;
//            
//        default:
//            break;
//    }
//    return type;
//}

- (void)requestReminderWithType:(NSInteger)remindtype page:(NSInteger)page pageSize:(NSInteger)pageSize complete:(void (^)(BOOL))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    
    NSString *url = [NSString stringWithFormat:@"%@/%ld",[YWCommonTool getReminderListUrl],(long)remindtype];
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    if (page) {
        [params setValue:@(page) forKey:@"page"];
    }
    if (pageSize) {
        [params setValue:@(pageSize) forKey:@"pageSize"];
    }
//    if (pullTime) {
//        [params setValue:@(pullTime) forKey:@"pullTime"];
//    }
    YWLog(@"url---%@,%@",url,params);
    [[WMHttpTool shareHttpTool] getWithUrl:url params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* reminderArray = [YWReminder mj_objectArrayWithKeyValuesArray:json];
        if (reminderArray.count) {
//            NSMutableArray *tempArr = [NSMutableArray array];
//            [tempArr addObjectsFromArray:reminderJson.remindMissionExps];
//            [tempArr addObjectsFromArray:reminderJson.remindMissionChanges];
//            [tempArr addObjectsFromArray:reminderJson.remindMissionCancels];
//            [tempArr addObjectsFromArray:reminderJson.remindKeepWatchNoBodies];
//            [tempArr addObjectsFromArray:reminderJson.remindInspectionNoBodies];
//            [tempArr addObjectsFromArray:reminderJson.remindDayReports];
//            [tempArr addObjectsFromArray:reminderJson.remindSystemNotices];
//            [tempArr addObjectsFromArray:reminderJson.remindAppVersionNotices];
            [[YWWorkReminderDBManager shareManager] updateReminderWithArray:reminderArray];
        }
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(NO);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (NSArray *)reminderWithPage:(NSInteger)page{
    return [[YWWorkReminderDBManager shareManager] reminderWithPage:page];
}

- (NSArray *)reminderWithType:(NSInteger)reminderType page:(NSInteger)page{
    return [[YWWorkReminderDBManager shareManager] reminderWithType:reminderType page:page];
}

- (BOOL)existUploadFailedMissions{
    return [[YWWorkReminderDBManager shareManager] existUploadFailedMissions];
}

- (void)requestReminderListWithComplete:(void (^)(BOOL))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSString *url = [YWCommonTool getReminderListUrl];
    
    YWLog(@"url---%@",url);
    [[WMHttpTool shareHttpTool] getWithUrl:url params:nil success:^(NSHTTPURLResponse* response, id json) {
        YWReminderList* reminderList = [YWReminderList mj_objectWithKeyValues:json];
        if (reminderList) {
            NSMutableArray *tempArr = [NSMutableArray array];
            if (reminderList.remindMissionExp) {
                [tempArr addObject:reminderList.remindMissionExp];
            }
            if (reminderList.remindMissionChange) {
                [tempArr addObject:reminderList.remindMissionChange];
            }
            if (reminderList.remindMissionCancel) {
                [tempArr addObject:reminderList.remindMissionCancel];
            }
            if (reminderList.remindKeepWatchNoBody) {
                [tempArr addObject:reminderList.remindKeepWatchNoBody];
            }
            if (reminderList.remindInspectionNoBody) {
                [tempArr addObject:reminderList.remindInspectionNoBody];
            }
            if (reminderList.remindDayReport) {
                [tempArr addObject:reminderList.remindDayReport];
            }
            if (reminderList.remindSystemNotice) {
                [tempArr addObject:reminderList.remindSystemNotice];
            }
            if (reminderList.remindAppVersionNotice) {
                [tempArr addObject:reminderList.remindAppVersionNotice];
            }
            //更新总数
            [[YWWorkReminderDBManager shareManager] updateReminderTotalCount:reminderList];
            //更新提醒
            [[YWWorkReminderDBManager shareManager] updateReminderWithArray:tempArr];
        }
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(NO);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"requestFailed", nil)];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

- (NSArray *)reminderListDateArray{
    return [[YWWorkReminderDBManager shareManager] reminderListDateArray];
}

- (BOOL)updateReminderLastTotalCountWithType:(NSInteger)reminderType{
    return [[YWWorkReminderDBManager shareManager] updateReminderLastTotalCountWithType:reminderType];
}

- (NSInteger)unreadRemindTotalCount{
    return [[YWWorkReminderDBManager shareManager] unreadRemindTotalCount];
}
- (BOOL)updateDailyReportWithDailyArray:(NSArray*)dailyArray{
    return [[YWWorkReminderDBManager shareManager] updateDailyReportWithDailyArray:dailyArray];
}

- (NSMutableDictionary*)dailyReportArrayWithDailyDate:(NSString*)analysisDate{
    return [[YWWorkReminderDBManager shareManager] dailyReportArrayWithDailyDate:analysisDate];
}

- (void)requestDailyReportWithAnalysisDate:(NSString *)analysisDate complete:(void (^)(BOOL succeed))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    NSString *url = [YWCommonTool getDayReportUrl];
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    if (analysisDate) {
        [params setValue:analysisDate forKey:@"analysisDate"];
    }
    YWLog(@"url---%@",url);
    [[WMHttpTool shareHttpTool] getWithUrl:url params:params success:^(NSHTTPURLResponse *response, id json) {
        NSArray * dailyArray = [YWDailyReport mj_objectArrayWithKeyValuesArray:json];
        [self updateDailyReportWithDailyArray:dailyArray];
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"requestFailed", nil)];
        }
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

@end
